package com.ruoyi.project.infoprojectcapitalannex.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.infoprojectcapitalannex.domain.InfoProjectCapitalAnnex;
import com.ruoyi.project.infoprojectcapitalannex.mapper.InfoProjectCapitalAnnexMapper;
import com.ruoyi.project.infoprojectcapitalannex.service.IInfoProjectCapitalAnnexService;
import com.ruoyi.project.infoprojectcapitalannex.vo.InfoProjectCapitalAnnexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


/**
 * 项目资金附件Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-09
 */
@Service
public class InfoProjectCapitalAnnexServiceImpl implements IInfoProjectCapitalAnnexService {
    @Autowired
    private InfoProjectCapitalAnnexMapper infoProjectCapitalAnnexMapper;

    /**
     * 查询项目资金附件
     *
     * @param id 项目资金附件主键
     * @return 项目资金附件
     */
    @Override
    public InfoProjectCapitalAnnex selectInfoProjectCapitalAnnexById(Long id) {
        return infoProjectCapitalAnnexMapper.selectInfoProjectCapitalAnnexById(id);
    }

    /**
     * 查询项目资金附件列表
     *
     * @param infoProjectCapitalAnnex 项目资金附件
     * @return 项目资金附件
     */
    @Override
    public List<InfoProjectCapitalAnnex> selectInfoProjectCapitalAnnexList(InfoProjectCapitalAnnex infoProjectCapitalAnnex) {
        return infoProjectCapitalAnnexMapper.selectInfoProjectCapitalAnnexList(infoProjectCapitalAnnex);
    }

    /**
     * 新增项目资金附件
     *
     * @param infoProjectCapitalAnnex 项目资金附件
     * @return 结果
     */
    @Override
    public int insertInfoProjectCapitalAnnex(InfoProjectCapitalAnnex infoProjectCapitalAnnex) {
        infoProjectCapitalAnnex.setCreateTime(DateUtils.getNowDate());
        return infoProjectCapitalAnnexMapper.insertInfoProjectCapitalAnnex(infoProjectCapitalAnnex);
    }

    /**
     * 修改项目资金附件
     *
     * @param infoProjectCapitalAnnex 项目资金附件
     * @return 结果
     */
    @Override
    public int updateInfoProjectCapitalAnnex(InfoProjectCapitalAnnex infoProjectCapitalAnnex) {
        infoProjectCapitalAnnex.setUpdateTime(DateUtils.getNowDate());
        return infoProjectCapitalAnnexMapper.updateInfoProjectCapitalAnnex(infoProjectCapitalAnnex);
    }

    /**
     * 批量删除项目资金附件
     *
     * @param ids 需要删除的项目资金附件主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectCapitalAnnexByIds(Long[] ids) {
        return infoProjectCapitalAnnexMapper.deleteInfoProjectCapitalAnnexByIds(ids);
    }

    /**
     * 删除项目资金附件信息
     *
     * @param id 项目资金附件主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectCapitalAnnexById(Long id) {
        return infoProjectCapitalAnnexMapper.deleteInfoProjectCapitalAnnexById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(InfoProjectCapitalAnnexVo infoProjectCapitalAnnexVo) {
        try {
            List<InfoProjectCapitalAnnex> projectCapitalAnnexes = infoProjectCapitalAnnexVo.getProjectCapitalAnnexes();
            for (InfoProjectCapitalAnnex projectCapitalAnnex : projectCapitalAnnexes) {
                projectCapitalAnnex.setId(null);
                projectCapitalAnnex.setProjectId(infoProjectCapitalAnnexVo.getProjectId());
                infoProjectCapitalAnnexMapper.insertInfoProjectCapitalAnnex(projectCapitalAnnex);
            }
            return 1;
        } catch (Exception e) {
            System.out.println(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }
}
